<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/13 0013
 * Time: 15:19
 */

namespace app\author\controller;

use think\Controller;

class MyBook extends Allow
{
    public function index(){
        $book_id = input("book_id");
        $book = db("book")->where('id',$book_id)->find();
        $chapter = db("chapter")
            ->order('index','desc')
            ->where('book_id',$book_id)
            ->paginate(15);
//        dump($chapter);die;
        $this->assign([
            'chapter' => $chapter,
            'total' => count($chapter),
            'book' => $book
        ]);
        return view("index");
    }
//    添加新章节
    public function add(){
        if(request()->post()){
            $param = request()->param();
            /************** 字数去掉空格 *********/
            $world_total = triMall($param['content']);
//            return $world_total;
            /**************字数去掉空格END********/
            /************ 敏感词过滤 **************/
            $getSensitive = rtrim($this->getSensitive(),"、");
            $getWord = explode("、",$getSensitive);
            $result = checkSensitive($getWord,$world_total);
            if($result['count'] >0){
                return ['state' => -2,'msg' => '检测到敏感词,'.$result['sensitiveWord']];
            }
            /************ 敏感词END **************/
            $content = [
                'content' => $param['content'],
                'words' => mb_strlen($world_total),
            ];
           $content_id = db("content") ->insertGetId($content);
           //查询这本书章节索引最大值
           $maxChapterIndex = db("chapter")->where('book_id',$param['book_id'])->max('index');
           //给这本书加上总字数
//            sleep(1);
            $font_word = db("book")->where('id',$param['book_id'])->setInc('words',mb_strlen($world_total));
            if(!$font_word){
                return ['state' => -1,'msg' => '网络延迟，请重试'];
            }
            $book = db("book")->where("id",$param['book_id'])->find();
           $data = [
                'book_id' => $param['book_id'],
                'name' => $param['chaptername'],
                'content_id' => $content_id,
                'is_free' => $book['auto_free'],
                'size' => mb_strlen($world_total),
                'index' => $maxChapterIndex+1
            ];
           if(empty($param['dingshi'])){
               $res =  db("chapter")->insert($data);
               if($res){
                   return ['state' => 200,'msg' => '章节发表成功'];
               }else{
                   return ['state' => -1,'msg' => 'ERROR'];
               }
           }else{
               $datas = [
                   'book_id' => $param['book_id'],
                   'name' => $param['chaptername'],
                   'content_id' => $content_id,
                   'is_free' => $book['auto_free'],
                   'size' => mb_strlen($world_total),
//                   'index' => $maxChapterIndex+1,
                   'time' => strtotime($param['dingshi']),
                   'author_id' => $book['m_id'],
               ];
               $res =  db("crontab_chapter")->insert($datas);
               if($res){
                   return ['state' => 200,'msg' => '章节已经加入到定时任务'];
               }else{
                   return ['state' => -1,'msg' => 'ERROR'];
               }
           }

        }
        $book_id = input("book_id");
//        $maxChapterIndex = db("chapter")->where('book_id',$book_id)->max('index')+1;
        $this->assign([
            'book_id' => $book_id
//            'chapter_title' => '第'.$maxChapterIndex.'章'
        ]);
        return view("add");
    }
    //    免费
    public function noCharge(){
        $id = input("id");
        $chapter = db("chapter")->where("id",$id)->update(['is_free'=>0]);
        if($chapter){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
//    收费
    public function charge(){
        $id = input("id");
        $chapter = db("chapter")->where("id",$id)->update(['is_free'=>1]);
        if($chapter){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
    //    删除
    public function delete(){
        $id = input("id");
        $del = db("chapter")->where("id",$id)->delete();
        $chapter = db("chapter")->find($id);
        db("content")->where("id",$chapter['content_id'])->delete();
        if($del){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => 'ERROR'];
        }
    }
//    章节修改
    public function edit(){
        $id = input("id");
        $chapter = db("chapter")->find($id);
        $content = db("content")->where('id',$chapter['content_id'])->find();
//        dump($chapter);die;
        if(request()->post()){
            $param = request()->param();
//            return $param;
            $chapter = db("chapter")->where('id',$param['chapter_id'])->update(['name' => $param['chaptername'],'size' => $param['words']]);
            $content = db("content")->where("id",$param['content_id'])->update(['content' => $param['content'],'words' => $param['words']]);
            if($chapter || $content){
                return ['state' => 200,'msg' => 'SUCCESS'];
            }else{
                return ['state' => -1,'msg' => 'ERROR'];
            }
        }
        $this->assign([
            'chapter' => $chapter,
            'content' => $content
        ]);
        return view("edit");
    }
    public function getSensitive(){
        if(session('sensitive_word')) return session('sensitive_word');
        $data = db("sensitive")->find(1);
        $word = triMall($data['word']);
        session('sensitive_word',$word);
        return $word;
    }
}
